package org.example.ttms.controller;

import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.layout.StackPane;
import javafx.util.Callback;
import org.example.ttms.customize.Detail;
import org.example.ttms.customize.Pop_ups;
import org.example.ttms.customize.Set;
import org.example.ttms.entityClass.User;
import org.example.ttms.entityClass.ticket.Ticket;
import org.example.ttms.entityClass.train.Route;
import org.example.ttms.entityClass.train.Trips;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class SearchTripsController {

    @FXML
    private StackPane stackPane;

    @FXML
    private ChoiceBox<String> choiceBox;

    @FXML
    private TextField textField;

    @FXML
    private Button searchButton;

    @FXML
    private TableView<Trips> tableView;

    @FXML
    private TableColumn<Trips, String> tripsIdTableColumn;

    @FXML
    private TableColumn<Trips, Integer> routeIdTableColumn;

    @FXML
    private TableColumn<Trips, String> goTimeTableColumn;

    @FXML
    private TableColumn<Trips, String> speedTimeTableColumn;

    @FXML
    private TableColumn<Trips, Integer> ticketNumTableColumn;

    @FXML
    private TableColumn<Trips, Integer> sellTicketNumTableColumn;

    @FXML
    private TableColumn<Trips, String> statusTableColumn;

    @FXML
    private TableColumn<Trips, Void> detailTableColumn;

    @FXML
    private TableColumn<Trips, Void> delTableColumn;

    @FXML
    private TableColumn<Trips, Void> setTableColumn;

    @FXML
    private Label label;

    public static ObservableList<Trips> observableList = FXCollections.observableArrayList();

    private String[] typeArray = {"车次号", "路线编号"};

    public String[] getTypeArray() {
        return typeArray;
    }

    public void setTypeArray(String[] typeArray) {
        this.typeArray = typeArray;
    }

    public void initialize(){
        Trips trips = new Trips();
        //choiceBox
        choiceBox.getItems().addAll(typeArray);
        choiceBox.getSelectionModel().select(0);
        //textField
        textField.setPromptText("输入车次号");
        choiceBox.valueProperty().addListener(((observableValue, oldValue, newValue) -> {
            if(newValue.equals("车次号")){
                textField.setPromptText("输入车次号");
            }
            else if(newValue.equals("路线编号")){
                textField.setPromptText("输入路线编号");
            }
        }));
        //tableView
        //数据
        tripsIdTableColumn.setCellValueFactory(cellData -> cellData.getValue().tripsIdStringProperty());
        routeIdTableColumn.setCellValueFactory(cellData -> cellData.getValue().routerIdProperty().asObject());
        goTimeTableColumn.setCellValueFactory(cellData -> cellData.getValue().goTimeProperty());
        speedTimeTableColumn.setCellValueFactory(cellData ->cellData.getValue().speedTimeProperty());
        ticketNumTableColumn.setCellValueFactory(cellData -> cellData.getValue().ticketAllNumProperty().asObject());
        sellTicketNumTableColumn.setCellValueFactory(cellData -> cellData.getValue().sellTicketNumProperty().asObject());
        statusTableColumn.setCellValueFactory(cellData -> cellData.getValue().statusProperty());
        //按钮
        detailTableColumn.setCellFactory(new Callback<TableColumn<Trips, Void>, TableCell<Trips, Void>>() {

            @Override
            public TableCell<Trips, Void> call(TableColumn<Trips, Void> tripsVoidTableColumn) {
                return new TableCell<Trips, Void>(){
                    private final Button detailButton = new Button("查看详情");
                    {
                        detailButton.setOnAction(e -> {
                            Detail detail = new Detail();
                            detail.ticketDetailWindow(getTableView().getItems().get(getIndex()));
                        });
                    }
                    @Override
                    public void updateItem(Void item, boolean empty){
                        super.updateItem(item, empty);
                        if(empty){
                            setGraphic(null);
                        }
                        else{
                            setGraphic(detailButton);
                        }
                    }
                };
            }
        });
        setTableColumn.setCellFactory(new Callback<TableColumn<Trips, Void>, TableCell<Trips, Void>>() {

            @Override
            public TableCell<Trips, Void> call(TableColumn<Trips, Void> tripsVoidTableColumn) {
                return new TableCell<Trips, Void>(){
                    private final Button setButton = new Button("  设置  ");
                    {
                        setButton.setOnAction(e -> {
                            Trips trips1 = getTableView().getItems().get(getIndex());
                            Pop_ups popUps = new Pop_ups();
                            if(trips1.getStatus().equals("出发")){
                                popUps.pop_ups3("该列车已经出发,无法设置状态");
                            }
                            else if(trips1.getStatus().equals("到达")){
                                popUps.pop_ups3("该列车已经到达,无法设置状态");
                            }
                            else {
                                Set set = new Set();
                                set.setStatusWindow(trips1, 2, textField.getText(), choiceBox.getValue());
                            }
                        });
                    }
                    @Override
                    public void updateItem(Void item, boolean empty){
                        super.updateItem(item, empty);
                        if(empty){
                            setGraphic(null);
                        }
                        else{
                            setGraphic(setButton);
                        }
                    }
                };
            }
        });
        delTableColumn.setCellFactory(new Callback<TableColumn<Trips, Void>, TableCell<Trips, Void>>() {
            @Override
            public TableCell<Trips, Void> call(TableColumn<Trips, Void> tripsVoidTableColumn) {
                return new TableCell<Trips, Void>(){
                    private final Button delButton = new Button("删除");
                    {
                        delButton.setOnAction(e -> {
                            Trips trips1 = getTableView().getItems().get(getIndex());
                            if(delTripsJudgement(trips1)){
                                //删除
                                observableList.remove(trips1);
                                //储存---更新数据
                                trips.delTrips(trips1);
                                //当火车未出发时删除,要减去该路线的车次数
                                if(!trips1.getStatus().equals("到达")) {
                                    trips.decreaseTripsNum(trips1);
                                }
                                //删除用户关于该车次的全部车票
                                User user = new User();
                                user.delTripsAllTicket(trips1);
                                //将该车次的车票全部删除
                                Ticket ticket = new Ticket();
                                ticket.delTripsAllTicket(trips1);

                            }
                        });
                    }
                    @Override
                    public void updateItem(Void item, boolean empty){
                        super.updateItem(item, empty);
                        if(empty){
                            setGraphic(null);
                        }
                        else{
                            setGraphic(delButton);
                        }
                    }
                };
            }
        });
        //label
        label.setVisible(observableList.isEmpty());
        observableList.addListener((ListChangeListener.Change<? extends Trips> c) -> {
            label.setVisible(observableList.isEmpty());
        } );
        tableView.setItems(observableList);
    }

    //车次删除评判
    public boolean delTripsJudgement(Trips trips){
        Pop_ups popUps = new Pop_ups();
        if(trips.getStatus().equals("出发")){
            popUps.pop_ups3("该车次已经出发,无法删除");
            return false;
        }
        if (!trips.getStatus().equals("到达") && trips.getSellTicketNum() != 0) {
            popUps.pop_ups3("该车次已有人买票,无法删除");
            return false;
        }
        popUps.pop_ups4("删除成功");
        return true;
    }
    @FXML
    void chickSearch(ActionEvent event) {
        Trips trips = new Trips();
        HashMap<String, Trips> hashMap = trips.searchTrips(textField.getText(), choiceBox.getValue());
        observableList.clear();
        observableList.addAll(hashMap.values());
    }

}
